#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "codeeditor.h"
#include "lex.h"
#include <QCheckBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    statusText=new QLabel();
    this->statusBar()->addWidget(statusText);
    statusText->setText("Program started");

    _editor=new CodeEditor;
    QGridLayout* layout=new QGridLayout();
    layout->addWidget(_editor, 0, 0, 1, 1);
    this->centralWidget()->setLayout(layout);
    connect(_editor, SIGNAL(textChanged()), this, SLOT(SetTitle()));

    _fileName="new_class.java";
    setWindowTitle(_fileName);
    _modified=false;
    _filePath="";
    showInfo=true;
    autoSave=false;

    infoWnd=new InfoWnd();
    parser=new Parser(infoWnd);
}

MainWindow::~MainWindow()
{
    delete statusText;
    delete _editor;
    delete ui;
}

void MainWindow::SetTitle()
{
    if(!_modified)
    {
        this->setWindowTitle(_fileName+"*");
        _modified=true;
    }
    statusText->setText("");
}

void MainWindow::NewFile()
{
    if(!SaveModified())
        return;
    _editor->clear();
    _fileName="new_class.java";
    setWindowTitle(_fileName);
    _modified=false;
    _filePath="";
    statusText->setText("New file generated seccessfuly");
}

void MainWindow::OpenFile()
{
    if(!SaveModified())
        return;
    QString fileName=QFileDialog::getOpenFileName(this, "Open file", _filePath, "Java class (*.java)");
    if(fileName.isNull())
        return;
    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QMessageBox msg;
        msg.setText("Cant open file for reading");
        msg.exec();
        return;
    }
    QTextStream stream(&file);
    _editor->clear();
    _editor->setPlainText(stream.readAll());
    file.close();
    int index=fileName.lastIndexOf("/");
    _filePath=fileName.left(index);
    _fileName=fileName.right(fileName.length()-index-1);
    _modified=false;
    setWindowTitle(_fileName);
    statusText->setText("File opened seccessfuly");
}

bool MainWindow::SaveFile()
{
    if(_filePath=="")
        return SaveAsFile();
    QFile file(_filePath+"/"+_fileName);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
    {
        QMessageBox msg;
        msg.setText("Cant open file for writing. Maybe it's write protected or you dont have permision");
        msg.exec();
        return false;
    }
    QTextStream stream(&file);
    stream<<_editor->toPlainText();
    file.close();
    _modified=false;
    setWindowTitle(_fileName);
    statusText->setText("File saved seccessfuly");
    return true;
}

bool MainWindow::SaveAsFile()
{
    QString fileName=QFileDialog::getSaveFileName(this, "Save file as...", _filePath, "Java class (*.java)");
    if(fileName.isNull())
        return false;
    if(!QFile::exists(fileName))
        if(!fileName.endsWith(".java"))
            fileName.append(".java");
    QFile file(fileName);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
    {
        QMessageBox msg;
        msg.setText("Cant open file for writing. Maybe it's write protected or you dont have permision");
        msg.exec();
        return false;
    }
    QTextStream stream(&file);
    stream<<_editor->toPlainText();
    file.close();
    int index=fileName.lastIndexOf("/");
    _filePath=fileName.left(index);
    _fileName=fileName.right(fileName.length()-index-1);
    _modified=false;
    setWindowTitle(_fileName);
    statusText->setText("File saves seccessfuly");
    return true;
}

void MainWindow::Build()
{
    if(!SaveModified())
        return;
    if(showInfo)
        infoWnd->show();
    if(parser->ParseFile(_filePath+"/"+_fileName))
        statusText->setText("Build seccessful");
    else
        statusText->setText("Build errors found");
}

void MainWindow::Exit()
{
    if(!SaveModified())
        return;
    close();
}

bool MainWindow::SaveModified()
{
    if(_modified)
    {
        int ret=QMessageBox::Save;
        if(!autoSave)
        {
            QMessageBox msg;
            QCheckBox* saveAlways=new QCheckBox("Save always");
            msg.layout()->addWidget(saveAlways);
            msg.setWindowTitle("The document has been modified");
            msg.setText("Do you want to save your changes?");
            msg.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel | QMessageBox::Discard);
            ret=msg.exec();
            if(ret==QMessageBox::Save)
                autoSave=saveAlways->checkState();
        }
        switch(ret)
        {
        case QMessageBox::Save:
            return SaveFile();
        case QMessageBox::Cancel:
            return false;
        default:
            return true;
        }
    }
    return true;
}

void MainWindow::ShowInfoChanged()
{
    showInfo=!showInfo;
}
